import { Context, Next } from "koa";
import { CheckType, getCtxParams } from "../util/param";
import {
  resultErrorJson,
  resultSuccessJson,
  resultType,
} from "../util/resultError";
import {
  AddContent,
  DeleteContent,
  SelectAllContent,
  SelectOneContent,
  UpdateContent,
  UpdateContentOpenTimes,
} from "../dao/content-dao";

export const AddContentService = async (ctx: Context, next: Next) => {

  const { check, error, params } = getCtxParams(ctx, [
    { keyname: "title", checkType: CheckType.cantEmpty },
    { keyname: "author", checkType: CheckType.cantEmpty },
    { keyname: "tip", checkType: CheckType.cantEmpty },
    { keyname: "fileUrl", checkType: CheckType.canEmpty },
    { keyname: "imgUrl", checkType: CheckType.cantEmpty },
    { keyname: "original", checkType: CheckType.cantEmpty },
    { keyname: "tag", checkType: CheckType.cantEmpty },
    { keyname: "isnote", checkType: CheckType.cantEmpty },
    { keyname: "demoUrl", checkType: CheckType.canEmpty },
  ]);
  console.log(params)
  if (!check) {
    ctx.body = error;
    return next;
  }
  try {
    await AddContent(params);
    const content = await SelectAllContent();
    ctx.body = resultSuccessJson(undefined, "内容添加成功", { content });
  } catch (error) {
    ctx.body = resultErrorJson(
      resultType.sqlError,
      `AddContentService Sql Error`,
      { error }
    );
  }
  return next;
};

export const GetContentService = async (ctx: Context, next: Next) => {
  const { check, error, params } = getCtxParams(ctx, [
    { keyname: "id", checkType: CheckType.canEmpty },
    { keyname: "isOpen", checkType: CheckType.canEmpty },
  ]);
  if (!check) {
    ctx.body = error;
    return next;
  }

  try {
    let content = [];
    // 有id则查询单个 没有则查询全部
    if (params.id) {
      // 查询单个时 先更新点击次数
      if (!!params.isOpen) {
        await UpdateContentOpenTimes(params);
      }

      content = await SelectOneContent(params);
    } else {
      content = await SelectAllContent();
    }
    ctx.body = resultSuccessJson(undefined, "内容查询成功", { content });
  } catch (error) {
    ctx.body = resultErrorJson(
      resultType.sqlError,
      `AddContentService Sql Error`,
      { error }
    );
  }
  return next;
};

export const DeleteContentService = async (ctx: Context, next: Next) => {
  const { check, error, params } = getCtxParams(ctx, [
    { keyname: "id", checkType: CheckType.cantEmpty },
  ]);
  if (!check) {
    ctx.body = error;
    return next;
  }
  try {
    await DeleteContent(params);
    ctx.body = resultSuccessJson(undefined, "内容删除成功", {});
  } catch (error) {
    ctx.body = resultErrorJson(
      resultType.sqlError,
      `AddContentService Sql Error`,
      { error }
    );
  }
  return next;
};

export const UpdateContentService = async (ctx: Context, next: Next) => {
  // http://localhost:8000/blog/UpdateContent?id=9&title=2&author=1&tip=1&fileUrl=1&imgUrl=1&original=1&tag=1
  const { check, error, params } = getCtxParams(ctx, [
    { keyname: "id", checkType: CheckType.cantEmpty },
    { keyname: "title", checkType: CheckType.cantEmpty },
    { keyname: "author", checkType: CheckType.cantEmpty },
    { keyname: "tip", checkType: CheckType.cantEmpty },
    { keyname: "fileUrl", checkType: CheckType.canEmpty },
    { keyname: "imgUrl", checkType: CheckType.cantEmpty },
    { keyname: "original", checkType: CheckType.cantEmpty },
    { keyname: "tag", checkType: CheckType.cantEmpty },
    { keyname: "isnote", checkType: CheckType.cantEmpty },
    { keyname: "demoUrl", checkType: CheckType.canEmpty },
  ]);
  if (!check) {
    ctx.body = error;
    return next;
  }

  try {
    await UpdateContent(params);
    const content = await SelectOneContent(params);
    ctx.body = resultSuccessJson(undefined, "内容更新成功", { content });
  } catch (error) {
    ctx.body = resultErrorJson(
      resultType.sqlError,
      `AddContentService Sql Error`,
      { error }
    );
  }
  return next;
};
